/**
 * Created by Joseph on 11/04/2015.
 */
(function () {
    'use strict';

    angular.module('app.auth')
        .controller('LoginCtrl', ['$scope', '$state', '$filter', '$timeout', 'AuthenticationService', 'SessionService', 'logger', LoginCtrl])
        .controller('ForgotCtrl', ['$scope', '$state', ForgotCtrl]);

    function LoginCtrl ($scope, $state, $filter, $timeout, AuthenticationService, SessionService, logger) {
        $scope.loginInfo = {};

        $scope.doLogin = function () {
            if ($scope.fmrLogin.$valid) {
                AuthenticationService.login($scope.loginInfo)
                .then(function(response) {
                    switch (response.status) {
                        case 204:
                            logger.logError($filter('translate')('PAGE_SIGNIN_ERROR_WRONG_CREDENTIALS'));
                        break;
                        case 200:
                            $timeout(function() {
                                logger.logSuccess($filter('translate')('PAGE_SIGNIN_WELCOME') + ' ' + angular.fromJson(response.data).Nombre);
                                $state.go('dashboard');
                            });
                        break;
                    }
                }, null);
            }
        };
    }

    function ForgotCtrl ($scope, $state) {
        $scope.email = '';

        $scope.doReset = function () {
            if ($scope.fmrLogin.$valid) {
//                alert($scope.email);
            }
        };
    }

})();
